package com.andnnl.sms;

import java.util.Arrays;

/**
 * Created by chenss on 2023/11/10.
 */
public class SmsCountTest {

    public static void main(String[] args) {
        String str1 = "1,2,3";
        String str2 = "1|2|3";
        String str3 = "1\\2\\3";
        System.out.println(str3);

        String[] split1 = str1.split(",");  // 使用逗号作为分隔符
        String[] split2 = str2.split("|");  // 使用竖线作为分隔符
        String[] split3 = str3.split("\\\\");  // 使用竖线作为分隔符

        System.out.println(Arrays.toString(split1));  // 输出：[1, 2, 3]
        System.out.println(Arrays.toString(split2));  // 输出：[1, |, 2, |, 3]
        System.out.println(Arrays.toString(split3));  // 输出：[1, 2, 3]
        String s = "";
        for (int i = 0; i < 3000; i++) {
            s += "1";
            int a1 = calSmsCount(s);
//            int a2=calculateSmsCount(s);
//            int a2=calculateCost(s);
            int a2 = calculateSMSCost3(s);
            if (a1 != a2) {
                System.out.println(s.length() + " " + a1 + " " + a2);
            }
        }
    }

    /**
     * 计算发送的长短信条数
     *
     * @param smsContent
     */
    public static int calSmsCount(String smsContent) {

        int msgCount = 1;

        // 70个字以内，按70来算，超过70按67来算
        if (smsContent.length() <= 70) {
            msgCount = 1;
        } else {
            msgCount = getMsgString(67, smsContent);
        }

        return msgCount;

    }

    private static int getMsgString(int radix, String content) {
        int contentLength = 0;
        contentLength = content.length();

        int size = contentLength / radix;
        size = (contentLength - size * radix) > 0 ? size + 1 : size;

        return size;
    }


    /**
     * 正确
     *
     * @param content
     * @return
     */
    public static int calculateSmsCount(String content) {
        int messageLength = content.length();
        if (messageLength <= 70) {
            return 1;
        } else {
            int smsCount = messageLength / 67;
            if (messageLength % 67 != 0) {
                smsCount++;
            }
            return smsCount;
        }
    }

    /**
     * 错误
     *
     * @param message
     * @return
     */
    public static int calculateCost(String message) {
        int count = message.length();
        int cost = 0;
        if (count <= 70) {
            cost = 1;
        } else {
            cost = (count + 63) / 67; // 向上取整
        }
        return cost;
    }

    /**
     * 错误
     *
     * @param message
     * @return
     */
    private static int calculateSMSCost(String message) {
        int messageLength = message.length();
        int cost = 1; // 初始费用为1条

        if (messageLength > 70) {
            // 超过70字符，按67字符/条计费
            cost = (int) Math.ceil((double) (messageLength - 70) / 67) + 1;
        }

        return cost;
    }

    /**
     * 正确
     *
     * @param message
     * @return
     */
    private static int calculateSMSCost2(String message) {
        int messageLength = message.length();
        int cost;

        if (messageLength <= 70) {
            cost = 1;
        } else {
            cost = (messageLength - 1) / 67 + 1;
        }

        return cost;
    }

    private static int calculateSMSCost3(String message) {
        return (message.length() <= 70) ? 1 : (message.length() - 1) / 67 + 1;
    }
}
